package br.edu.ufcg.boombox.view;

import br.edu.ufcg.boombox.controller.LoginController;
import br.edu.ufcg.boombox.model.LoginModel;
import br.edu.ufcg.boombox.util.Validadores;
import javax.swing.JOptionPane;

/**
 *
 * @author Silva
 */
public class LoginView extends javax.swing.JFrame {
    
    
    /**
     * Creates new form LoginView
     */
    public LoginView() {
        this.setLocation(300, 100);
        initComponents();
        initActionComand();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        labelTextoCadastro = new javax.swing.JLabel();
        buttonCadastrar = new javax.swing.JButton();
        labelEmail = new javax.swing.JLabel();
        fieldEmail = new javax.swing.JTextField();
        labelSenha = new javax.swing.JLabel();
        passfieldSenha = new javax.swing.JPasswordField();
        buttonEntrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BoomBox");
        setBackground(new java.awt.Color(244, 244, 244));
        setMinimumSize(new java.awt.Dimension(600, 400));

        jPanel1.setBackground(new java.awt.Color(38, 115, 191));

        labelTextoCadastro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelTextoCadastro.setForeground(new java.awt.Color(238, 234, 234));
        labelTextoCadastro.setText("Novo no BoomBox? Cadastre-se gratuitamente!");

        buttonCadastrar.setText("Cadastrar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(labelTextoCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addComponent(buttonCadastrar)
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTextoCadastro, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(buttonCadastrar))
                .addContainerGap())
        );

        labelEmail.setText("E-mail:");

        labelSenha.setText("Senha:");

        buttonEntrar.setText("Entrar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelEmail)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelSenha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(passfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(buttonEntrar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(42, 42, 42))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(101, 101, 101)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEmail)
                    .addComponent(fieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenha)
                    .addComponent(passfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonEntrar)
                .addGap(0, 97, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LoginView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCadastrar;
    private javax.swing.JButton buttonEntrar;
    private javax.swing.JTextField fieldEmail;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JLabel labelTextoCadastro;
    private javax.swing.JPasswordField passfieldSenha;
    // End of variables declaration//GEN-END:variables

    
    /**
     * Inicializa o comando do componente
     */
    public void initBotaoCadastrar() {
        this.buttonCadastrar.setActionCommand("cadastrar");
    }
    
    /**
     * Inicializa o comando do componente
     */
    public void initBotaoEntrar() {
        this.buttonEntrar.setActionCommand("logar");
    }
    
    /**
     * Inicializa o comando do componente
     */
    public void initTextFieldEmail() {
        this.fieldEmail.setActionCommand("fieldEmail");
    }
    
    /**
     * Inicializa o comando do componente
     */
    public void initPassFieldSenha() {
        this.passfieldSenha.setActionCommand("fieldSenha");
    }
    
    private void initActionComand() {
        initBotaoCadastrar();
        initBotaoEntrar();
        initTextFieldEmail();
        initPassFieldSenha();
    }
    
    /**
     * Adiciona o controlador aos botoes e campos de texto usados.
     * @param controlador 
     */
    public void configuraOuvinte(LoginController controlador) {
        this.buttonCadastrar.addActionListener(controlador);
        this.buttonEntrar.addActionListener(controlador);
        this.passfieldSenha.addActionListener(controlador);
        this.fieldEmail.addActionListener(controlador);
    }

    public LoginModel lerModelo() {
        
        String email = this.fieldEmail.getText();
        String senha = new String(this.passfieldSenha.getPassword());
        
        
        if(!Validadores.validarEmail(email)) {
            JOptionPane.showMessageDialog(null, "Insira um email válido!");
        } else if( !(senha.length() >= 6 && senha.length() <= 12) ) {
            JOptionPane.showMessageDialog(null, "A senha deve possuir dentre seis a doze caracteres!");

        } else if ( Validadores.validarSenhaForte(senha) ){
            LoginModel modelo = new LoginModel();
            modelo.setEmail(email);
            modelo.setSenha(senha);
            return modelo;
        }
        
        return null;
    }

    public void clear() {
        this.fieldEmail.setText("");
        this.passfieldSenha.setText("");
    }
    
    
}
